package cn.hxzy.kaoshi.service.impl;

import cn.hxzy.kaoshi.entity.SysExam;
import cn.hxzy.kaoshi.entity.SysExamResults;
import cn.hxzy.kaoshi.mapper.SysExamMapper;
import cn.hxzy.kaoshi.mapper.SysExamResultsMapper;
import cn.hxzy.kaoshi.service.ISysExamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 考试管理 服务实现类
 * </p>
 *
 * @author tyqw
 * @since 2024-12-13
 */
@Service
public class SysExamServiceImpl implements ISysExamService {
    private static final Logger logger = LoggerFactory.getLogger(SysExamServiceImpl.class);

    @Autowired
    private SysExamMapper sysExamMapper;

    @Autowired
    private SysExamResultsMapper sysExamResultsMapper;

    // 根据班级编号查询考试列表
    @Override
    public PageInfo<Map> fselectAll(Map<String, Object> classId) {
        // 取出班级ID
        Integer pageNum = Integer.valueOf(String.valueOf(classId.get("pageNum")));
        Integer pageSize = Integer.valueOf(String.valueOf(classId.get("pageSize")));
        // 打印取出的数据
        PageHelper.startPage(pageNum, pageSize);
        List<Map> maps = sysExamMapper.fselectAll(classId);
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map map : maps) {
                Date startTime = dateFormat.parse(map.get("start_time").toString());
                Date endTime = dateFormat.parse(map.get("end_time").toString());
                Date currentTime = new Date();
                if (currentTime.before(startTime)) {
//                    System.out.println("当前时间在考试开始时间之前");
                    map.put("hasResult", false);
                } else if (currentTime.after(endTime)) {
//                    System.out.println("当前时间在考试结束时间之后");
                    map.put("hasResult", false);
                } else {
//                    System.out.println("当前时间在开始时间和结束时间之间");
                    Long examId = Long.valueOf(String.valueOf(map.get("exam_id")));
                    QueryWrapper<SysExamResults> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("student_id", classId.get("uid"));
                    queryWrapper.eq("exam_id", examId);
                    List<SysExamResults> sysExamResults = sysExamResultsMapper.selectList(queryWrapper);
                    if (!sysExamResults.isEmpty()) {
                        map.put("hasResult", false);
                    } else {
                        map.put("hasResult", true);
                    }
                }
            }
            return new PageInfo<>(maps);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageInfo<Map> findExaminations(Map<String, Object> map) {
        // 获取分页参数，默认值为第1页，每页5条记录
        Integer pageNum = Integer.valueOf(String.valueOf(map.get("pageNum")));
        Integer pageSize = Integer.valueOf(String.valueOf(map.get("pageSize")));
        // 执行分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Map> examinations = sysExamMapper.findExaminations(map);
        Boolean banduan = true;
        for (Map map1 : examinations) {
            String examinationId = map1.get("examination_id").toString();
            //查询有没有考试记录以及考试数据
            QueryWrapper<SysExamResults> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("examination_id", examinationId);
            List<SysExamResults> sysExamResults = sysExamResultsMapper.selectList(queryWrapper);
            //查询有没有考场有没有使用卷子
            QueryWrapper<SysExam> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("paper_id", examinationId);
            List<SysExam> sysExams = sysExamMapper.selectList(queryWrapper1);
            //返回结果
            if (sysExamResults.size() > 0 || sysExams.size() > 0) {
                banduan = false;
            } else {
                banduan = true;
            }
            map1.put("hasResult", banduan);
        }
        // 返回分页结果
        return new PageInfo<>(examinations);

    }

    @Override
    public List<Map> findClass() {
        return sysExamMapper.findClass();
    }

    @Override
    public SysExam fselectAll2(Integer classId) {
        SysExam sysExam = sysExamMapper.selectById(classId);
        return sysExam;
    }

    @Override
    public Map<String, Object> xueshichaban(Long userid) {
        return sysExamMapper.xueshichaban(userid);
    }

    @Override
    public void insertOrUpdate(SysExam sysExam) {
        if (sysExam.getExamId() != null) {
            sysExamMapper.updateById(sysExam);
        } else {
            sysExamMapper.insert(sysExam);
        }
    }

    @Override
    public void deleteExam(Long examId) {
        sysExamMapper.deleteExam(examId);
    }
}
